package com.sesame.mq.rabbit.topic.config;

import lombok.extern.apachecommons.CommonsLog;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;


/**
 * topic
 * 匹配
 */
@CommonsLog
@Component
public class RabbitTopicConfig {

    public static final String USER_QUEUE_1 = "topicQueue1";
    public static final String USER_QUEUE_2 = "topicQueue2";
    public static final String USER_QUEUE_3 = "topicQueue3";

    //直连交换机
    public static final String exchange = "topicUserExchange";

    //队列1
    @Bean
    public Queue topicQueue1() {
    /* 是否持久化
     仅创建者可以使用的私有队列，断开后自动删除
     当所有消费客户端连接断开后，是否自动删除队列*/
        return new Queue(USER_QUEUE_1, true, false, false);
    }

    //队列2
    @Bean
    public Queue topicQueue2() {
        return new Queue(USER_QUEUE_2, true, false, false);
    }

    @Bean
    public Queue topicQueue3() {
        return new Queue(USER_QUEUE_3, true, false, false);
    }


    //定义直连交换机
    @Bean
    public TopicExchange topicExchange() {
//        return new TopicExchange(exchange, true, false);
        return new TopicExchange(exchange);
    }

    //将队列1绑定到直接交换机
    /*
        *表示一个词.
        #表示零个或多个词.
    */
    @Bean
    public Binding bindTopicQueue1() {
        return BindingBuilder.bind(topicQueue1()).to(topicExchange()).with("topic.*");
    }

    @Bean
    public Binding bindTopicQueue2() {
        return BindingBuilder.bind(topicQueue2()).to(topicExchange()).with("topic.#");
    }
    @Bean
    public Binding bindTopicQueue3() {
        return BindingBuilder.bind(topicQueue3()).to(topicExchange()).with("aaa.bbb");
    }


}
